package engine.ui.equipStrenGthen 
{
	import engine.data.ModelLocator;
	import engine.data.View;
	import engine.net.messages.CustomToServerFlag;
	import engine.net.messages.SystemMsgFlag;
	import engine.net.mgr.SystemSender;
	import flash.display.MovieClip;
	import flash.events.MouseEvent;
	import newx.BaseObject;
	import newx.com.config.StaticIni;
	import newx.com.control.button.NxButton;
	import newx.com.control.group.NxViewGroup;
	import newx.com.control.NxClip;
	import newx.com.control.text.NxLabel;
	/**
	 * ...
	 * @author ...WangHaiBo 
	 * ...........www.woniu.com
	 * 装备镶嵌
	 */
	public class EquipEnchaseLogic implements IEquipLogic 
	{
		private static var _self					:EquipEnchaseLogic;
		private var viewgroup_left2_1		:NxViewGroup;
		private var viewgroup_left2_2		:NxViewGroup;
		private var lab_succeedpercent_2	:NxLabel;
		private var lab_needmoney_2			:NxLabel;
		private var btn_star_2				:NxButton;
		
		
		private var _viewGroupArray			:Array;
		private var _rockViewData			:BaseObject	=	null;
		private var _equipViewData			:BaseObject	=	null;
		private var _model					:ModelLocator;
		public function EquipEnchaseLogic(val:MovieClip) 
		{
			_model					=	ModelLocator.getInstance();
			_viewGroupArray			=	[];
			viewgroup_left2_1		=	val.getChildByName("viewgroup_left2_1") 		as NxViewGroup;
			viewgroup_left2_2		=	val.getChildByName("viewgroup_left2_2")			as NxViewGroup;
			lab_succeedpercent_2	=	val.getChildByName("lab_succeedpercent_2") 		as NxLabel;
			lab_needmoney_2			=	val.getChildByName("lab_needmoney_2") 			as NxLabel;
			btn_star_2				=	val.getChildByName("btn_star_2")				as NxButton;
			_viewGroupArray			=	[null, viewgroup_left2_1, viewgroup_left2_2];
			addListent();
		}
		/**
		 * 界面显示类容，需要同时修改
		 * @param	dis
		 * @return
		 */
		public static function getInstance(dis:MovieClip):EquipEnchaseLogic
		{
			if (_self == null) 
			{
				_self	=	new EquipEnchaseLogic(dis);
			}
			return	_self;
		}
		/**
		 * 添加装备
		 * @param	val
		 */
		public function addEquip(val:BaseObject):void
		{
			var viewIndex:int	=	int(val["ObjKey"]);
			var item:MovieClip	=	(_viewGroupArray[viewIndex] as NxViewGroup).getItemByIndex(0);
			while (item.numChildren > 1)
			{
				item.removeChildAt(1);
			}
			var equipClip:NxClip	=	new NxClip();
			equipClip.width			=	equipClip.height	=	40;
			if (viewIndex	== 2) 
			{
				equipClip.x				=	2;
				equipClip.y				=	2;
				_rockViewData	=	val;
			}
			else if (viewIndex	==	1)
			{
				equipClip.x				=	6;
				equipClip.y				=	5;
				_equipViewData	=	val;
			}
			val.HandleProperties("NowViewType",View.VIEWPORT_ENCHASE)
			item["data"]			=	val;
			equipClip.source		=	"res/items/icon_" + val.GetProperties("Photo") + ".jpg";
			item.addChild(equipClip);
			needMenoy();
			succeedPercent();
			checkStarBtnEnabled();
		}
		/**
		 * 删除装备
		 * @param	val
		 */
		public function removeEquip(val:BaseObject):void
		{
			var viewIndex:int	=	int(val["ObjKey"]);
			var item:MovieClip	=	(_viewGroupArray[viewIndex] as NxViewGroup).getItemByIndex(0);
			while (item.numChildren > 1)
			{
				item.removeChildAt(1);
			}
			item["data"]			=	null;
			if (viewIndex	==	3) 
			{
				_rockViewData	=	null
				
			}
			else if (viewIndex	==	1)
			{
				_equipViewData	=	null;
			}
			needMenoy();
			succeedPercent();
			checkStarBtnEnabled();
		}
		/**
		 * 清空所有的显示信息
		 */
		public function removeAll():void
		{
			for each (var item:NxViewGroup in _viewGroupArray) 
			{
				if (item != null) 
				{
					var itemMc:MovieClip	=	item.getItemByIndex(0);
					while (itemMc.numChildren > 1)
					{
						itemMc.removeChildAt(1);
					}
					itemMc["data"]			=	null;
				}
			}
			_rockViewData	=	null;
			_equipViewData	=	null;
			lab_needmoney_2.htmlText		=	"";
			lab_succeedpercent_2.htmlText	=	"";
			btn_star_2.enabled				=	false;
		}
		/**
		 * 镶嵌所要的钱数
		 * @param	val
		 */
		private function needMenoy():void
		{
			var tempMoney:int	=	0;
			var menoyStr:String	=	"";
			
			if(_rockViewData != null && _rockViewData != null)
			{
				if(_rockViewData.CheckProperties("ConfigID"))
				{
					var configID:String	=	_rockViewData.GetProperties("ConfigID");
					var checkRockObj:Object	=	StaticIni.getIniObj(configID)
					tempMoney	=	checkRockObj["EnchaseCapitalValue"];
				}
				menoyStr	=	String(tempMoney);
				if (tempMoney <= _model.scene.Role.getGoldCoin()) 
				{
					menoyStr	=	"<font color='#00FF40'>"	+	menoyStr	+	"</font>"
				}
				else
				{
					menoyStr	=	"<font color='#FF0000'>"	+	menoyStr	+	"</font>"
				}
				lab_needmoney_2.htmlText		=	StaticIni.getSwfLangStrVar("F_Gem_13222", [menoyStr])	+	StaticIni.getSwfLangStr("CapitalType_0");
			}
			else
			{
				lab_needmoney_2.htmlText		=	"";
			}
		}
		/**
		 * 成功率
		 */
		private function succeedPercent():void
		{
			var succeedStr:String	=	"";
			if(_rockViewData != null && _rockViewData != null)
			{
				succeedStr	=	"<font color='#00FF40'>100%</font>";
				lab_succeedpercent_2.htmlText	=	StaticIni.getSwfLangStrVar("NewItemChangeUIStr_36", [succeedStr]);
			}
			else
			{
				lab_succeedpercent_2.htmlText	=	succeedStr;
			}
			
		}
		/**
		 * 按钮是否可用
		 */
		private function checkStarBtnEnabled():void
		{
			if(_rockViewData != null && _rockViewData != null)
			{
				btn_star_2.enabled	=	true;
			}
			else
			{
				btn_star_2.enabled	=	false;
			}
		}
		/**
		 * 开始镶嵌
		 * @param	e
		 */
		private function starBtnClickHandle(e:MouseEvent):void
		{
			SystemSender.getInstance().CustomMsg(CustomToServerFlag.CLIENT_CUSTOMMSG_ENCHASE);
		}
		/**
		 * 添加事件
		 */
		private function addListent():void
		{
			btn_star_2.addEventListener(MouseEvent.CLICK, starBtnClickHandle);
		}
		/**
		 * 删除事件
		 */
		private function removeListent():void
		{
			btn_star_2.removeEventListener(MouseEvent.CLICK, starBtnClickHandle);
		}
		
	}

}